//全局变量descn
var descnShow = '';
var descnWebShow = '';
var _imgesByUrl = 'http://img.boyitongcheng.com/';
var showMainCity = '成都市';
$(function(){
    $.ajax({
        type: 'get',
        url: '/loginWeb/isLogin.json',
        dataType: 'json',
        data: {},
        success: function (ret) {
            if (ret.isLogin == 'OK') {
                queryTrollyNum();
            } else {
                $(".cartDiv em").hide();
            }
        },
        error: function(e) {
            console.log('error')
        }
    });
});

function queryTrollyNum(){
    $.post("/htmlWeb/trolleySum",function(data, textStatus, jqXHR){
        $(".cartDiv em").html(data);
        if(data > 0){
            $(".cartDiv em").fadeIn();
        }else{
            $(".cartDiv em").hide();
        }
    });
}
//首次加载主页面
$(".mainDiv").load("indexHtml",'',function(){
    // $(".mainDiv").load(url,'',function(){});
    //轮播样式
    // jQuery(".bannerDiv,.plaBann").slide({
    //     mainCell:".bd ul",
    //     effect:"leftLoop",
    //     autoPlay:true
    // });
    // //tab切换
    // var $tab_li = $('.platab_menu ul li');
    // $tab_li.click(function(){
    //     $(this).addClass('selected').siblings().removeClass('selected');
    //     var index = $tab_li.index(this);
    //     $('div.platab_box > div').eq(index).show().siblings().hide();
    // });

    listDivShow();
    // particUlDiv();
    toOrderPage();
});

//菜单点击事件main切换页面(路由\动态加载js)
var navPills = function(url){
    if(url){
        var jsName = "";
        if(url.indexOf("system") != -1){
            var rePath = url.split("system")[1];
            jsName = "system/system_" + rePath.substring(0,1).toLowerCase() + rePath.substring(1);
        }else{
            jsName = url.split("Html")[0];
        }

        var jsPath = "../../by-webhtml/assets/js/" + jsName + ".js";
        if(url == 'indexHtml'){
            listDivShow();
        }else {
            listDivHidden();
        }
        $(".mainDiv").load(url,'',function(){
            if(url != 'indexHtml'){
                jQuery(".bannerDiv,.plaBann").slide({
                    mainCell:".bd ul",
                    effect:"leftLoop",
                    autoPlay:true
                });
            }
            $.getScript(jsPath);
        });
    }

};

//拦截本地跳转参数
function getUrlParam(name){
    var LocString = String(window.document.location.href);
    var rs = new RegExp("(^|)" + name + "=([^&]*)(&|$)", "gi").exec(LocString);
    if(rs == null || rs.length < 3){
        return "";
    }
    return decodeURI(rs[2]);
}

function toOrderPage(){

    if(getUrlParam("flag").indexOf('trolley') != -1){
        $(".mainDiv").load("cartHtml",'',function(){
            setTimeout(function(){
                listDivHidden();
            },500);
            $.getScript("../../by-webhtml/assets/js/cart.js");
        });
    }else if(getUrlParam("flag").indexOf('caseDetail') != -1){
        var city = getUrlParam("city");
        $('.mainDiv').load('case_DetailsHtml', {}, function(){
            setTimeout(function(){
                listDivHidden();
            },500);
            $("#mainCity span").html(city);
            showMainCity = city;
            $("#caseIdHidden").val(getUrlParam("caseId"));
            setTimeout(function(){
                $.getScript("../../by-webhtml/assets/js/case_Details.js");
            },100);

        });
    }
}

//跳转后台我的订单
function goMyOrder(){
    $.ajax({
        type: 'get',
        url: '/loginWeb/isLogin.json',
        dataType: 'json',
        data: {},
        success: function (ret) {
            if (ret.isLogin == 'OK') {
                window.location.href = "/systemMainHtml?flag='order'";
            } else {
                navPills('loginHtml');
            }
        },
        error: function(e) {
            console.log('error')
        }
    });

}

//显示所有服务分类显示
var listDivShow = function(){
    $(".listDiv").children('#list_nav').slideDown();
    $(".listDiv").children("h1").addClass("cur");
    $('.list_nav').attr('id','list_navNo');

    $("#list_nav ul li").hover(function(){
        $(this).addClass('cur');
//		$(this).children(".Menu").animate({top:0,queue:true});
        $(this).children(".Menu").show();

    },function(){
        $(this).removeClass('cur');
//		$(this).children(".Menu").animate({top:50,queue:true});
        $(this).children(".Menu").hide();
    });

    $(".list_nav ul li").hover(function(){
        $(this).addClass('cur');
//		$(this).children(".Menu").animate({top:0,queue:true});
        $(this).children(".Menu").show();

    },function(){
        $(this).removeClass('cur');
//		$(this).children(".Menu").animate({top:50,queue:true});
        $(this).children(".Menu").hide();
    });
};

//隐藏所有服务分类显示并由鼠标控制显示隐藏
var listDivHidden = function(){
    $('.list_nav').attr('id','list_nav');
    $(".listDiv").children('#list_nav').slideUp();
    $(".listDiv").children("h1").removeClass("cur");

    // var mark=1;
    // alert(mark);
    // console.log(mark);
    $(".listDiv").hover(function(){
        setTimeout(function(){
            var mark=1;
            if(mark==1){//把他展开
                $(this).children('#list_nav').slideDown();
                $(this).children("h1").addClass("cur");
                return mark=2;
            }else if(mark==2){//收缩
                $(this).children('#list_nav').slideUp();
                $(this).children("h1").removeClass("cur");
                return mark=1;
            }
        },200);//延时3秒

    });

    $("#list_nav ul li").hover(function(){
        $(this).addClass('cur');
//		$(this).children(".Menu").animate({top:0,queue:true});
        $(this).children(".Menu").show();

    },function(){
        $(this).removeClass('cur');
//		$(this).children(".Menu").animate({top:50,queue:true});
        $(this).children(".Menu").hide();
    });

    $(".list_nav ul li").hover(function(){
        $(this).addClass('cur');
//		$(this).children(".Menu").animate({top:0,queue:true});
        $(this).children(".Menu").show();

    },function(){
        $(this).removeClass('cur');
//		$(this).children(".Menu").animate({top:50,queue:true});
        $(this).children(".Menu").hide();
    });
};

//判断是否登录
var mainHtmlTopRightFunction = function(){
    $.ajax({
        url:"loginWeb/isLogin.json",
        type:'GET',
        dataType:"json",
        success:function(result){
            var tem = "";
            if(result.isLogin == 'OK'){
                tem += "<span>您好，"+result.userName+" </span><a class='cur' href=\"/systemMainHtml\">我的博艺</a>" +
                    "<a href=\"#\" onclick=\"goMyOrder()\">我的订单</a><a class='uploat_App cur' href=\"#\" onclick=\"navPills('uploadAppHtml');\">下载APP</a>";
            }else {
                tem += "<span>您好，</span>" +
                    "<a class='cur' href=\"#\" onclick=\"navPills('loginHtml');\">请登录</a>" +
                    "<a href=\"#\" onclick=\"navPills('registerHtml');\">会员注册</a>" +
                    "<a href=\"#\" onclick=\"navPills('tenantsHtml');\">商家入驻</a>" +
                    "<a href=\"#\" onclick=\"goMyOrder()\">我的订单</a>" +
                    "<a class='uploat_App cur' href=\"#\" onclick=\"navPills('uploadAppHtml');\">下载APP</a>";
            }
            $("#mainHtmlTopRight").empty();
            $("#mainHtmlTopRight").append(tem);
        }
    });
}();

//获取导航栏
var topListFunction = function(){
    $.ajax({
        url:"getTopList.json",
        type:'GET',
        dataType:"json",
        success:function(result){
            var tem = "<li role='presentation' class='active'><a href=\"#\" onclick=\"navPills('indexHtml');\">首页</a></li>";
            // var tem = "<li role='presentation' class='active'><a href=\"javascript:;\">首页</a></li>";
            $.each( result.topList, function(index, content){
                // tem += "<li role='presentation'><a href=\"#\" onclick=\"navPills('"+content.url+"');\">"+content.name+"</a></li>";
                tem += "<li role='presentation' channel_id='" + content.id + "'><a href=\"javascript:;\">"+content.name+"</a></li>";
            });
            tem += "<li role='presentation'><a href=\"#\" onclick=\"navPills('aboutHtml');\">关于我们</a></li>";
            $(".nav-pills").empty();
            $(".nav-pills").append(tem);
        }
    });
}();

//获取菜单栏
var leftListFunction = function(){
    $.ajax({
        url:"getLeftList.json",
        type:'GET',
        dataType:"json",
        success:function(result){
            var tem = "";
            var leftList = result.leftList;
            $.each( leftList, function(index, content){
                // tem += "<li class='"+content.imgUrl+"'><a href='"+content.url+"'><span>"+content.name+"</span></a><div class='Menu cleafix'>";
                tem += "<li class='"+content.imgUrl+"'><a><span>"+content.name+"</span></a><div class='Menu cleafix'>";
                if(content.channelList != null){
                    var leftSubMap = content.channelList;
                    $.each( leftSubMap, function(index, contentSb){
                        tem += "<h5>"+contentSb.name+"</h5><p>";
                        if(contentSb.channelList != null){
                            var leftSubKWMap = contentSb.channelList;
                            $.each( leftSubKWMap, function(index, contentSbKW){
                                tem += "<a onclick=\"gaogFunctionPage(" + (contentSbKW.id-1000000) + ");\">"
                                    +contentSbKW.name+"</a>";
                            });
                        }
                        tem += "</p>";
                    });
                }
                tem += "</div></li>";
            });

            $("#list_navCleafix").empty();
            $("#list_navCleafix").append(tem);

            //动态加载样式
            $("<link>").attr({ rel: "stylesheet",
                type: "text/css",
                href: "../../by-webhtml/assets/css/main.css"
            }).appendTo("head");
            listDivHidden();
            listDivShow();
        }
    });
}();